
/** 
* @Description: (用一句话描述该文件做什么) 
* @author heliang
* @date 2019-8-28 上午10:52:37 
* @version Bate1.0
*/

package org.alipay4j.component.life;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alipay4j.Alipay;
import org.alipay4j.AlipayException;
import org.alipay4j.Configuration;
import org.alipay4j.component.AbstractComponent;
import org.alipay4j.http.AlipayHttpsClient;
import org.alipay4j.http.Response;
import org.alipay4j.model.group.Group;
import org.alipay4j.model.menu.LabelRule;
import org.alipay4j.util.ObjectHelper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


/** 
 * @ClassName: 分组组件 
 * @Description:  负责用户分组操作
 * @author heliang
 * @date 2019-8-28 上午10:52:37 
 * @version V2.1 * Update Logs: * Name: * Date: * Description: 初始化
 */
public class GroupComponent extends AbstractComponent {

    public GroupComponent(Alipay alipay) {
        super(alipay);
    }
    
    /**
    * @Title: 查询用户分组列表 
    * @Description: 使用该接口查询分组列表，并配合其它分组管理接口使用
    * @author heliang
    * @return List<Group>
    * @throws AlipayException 
     */
    public List<Group> batchquery() throws AlipayException {
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("method", "alipay.open.public.group.batchquery");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_group_batchquery_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_group_batchquery_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        List<Group> groups = new ArrayList<Group>();
        JSONArray array = bodyRes.getJSONArray("groups");
        for(int i=0;i<array.size();i++) {
            Group group = JSONObject.toJavaObject(array.getJSONArray(i), Group.class);
            groups.add(group);
        }
        return groups;
    }
    
    /**
    * @Title: 创建分组 
    * @Description: 创建分组
    * @author heliang
    * @param name 分组名
    * @param rules 规则
    * @return group_id 分组id
    * @throws AlipayException 
     */
    public String careate(String name,List<LabelRule> rules) throws AlipayException {
        if (ObjectHelper.isEmpty(name)) {
            throw new IllegalArgumentException("name can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("name", name);
        body.put("label_rule", JSONObject.toJSONString(rules));
        body.put("method", "alipay.open.public.group.create");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_group_create_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_group_create_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return bodyRes.getString("group_id");
    }
    
    /**
    * @Title: 用户分组修改接口 
    * @Description: 调用改接口修改用户分组，包括分组名称以及分组规则
    * @author heliang
    * @param group_id 分组ID，整型值
    * @param name 分组名称，仅支持中文、字母、数字、下划线的组合。
    * @param rules 标签规则，满足该规则的粉丝将被圈定，标签id不能重复   
    * @return boolean
    * @throws AlipayException 
     */
    public boolean modify(String group_id,String name,List<LabelRule> rules) throws AlipayException {
        if (ObjectHelper.isEmpty(group_id)) {
            throw new IllegalArgumentException("group_id can't be null or empty");
        }
        if (ObjectHelper.isEmpty(name)) {
            throw new IllegalArgumentException("name can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("group_id", group_id);
        body.put("name", name);
        body.put("label_rule", JSONObject.toJSONString(rules));
        body.put("method", "alipay.open.public.group.modify");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_group_modify_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_group_modify_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return true;
    }
    
    /**
    * @Title: 删除分组 
    * @Description: 调用接口删除用户分组
    * @author heliang
    * @param group_id 分组 ID
    * @return boolean
    * @throws AlipayException 
     */
    public boolean delete(String group_id) throws AlipayException {
        if (ObjectHelper.isEmpty(group_id)) {
            throw new IllegalArgumentException("group_id can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("group_id", group_id);
        body.put("method", "alipay.open.public.group.delete");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_group_delete_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_group_delete_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return true;
    }
    
    
    /**
    * @Title: 人群数量查询 
    * @Description: 用户可以指定自己创建的分组，调用接口查询对应的人群数量
    * @author heliang
    * @param rules 用户分组的规则项列表，规则项之间元素是与的逻辑，每个规则项内部用多个值表示或的逻辑
    * @return count 分组圈出的人群数量
    * @throws AlipayException 
     */
    public String crowdQuery(List<LabelRule> rules) throws AlipayException {
        if (ObjectHelper.isEmpty(rules)) {
            throw new IllegalArgumentException("rules can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("label_rule", JSONObject.toJSONString(rules));
        body.put("method", "alipay.open.public.group.crowd.query");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_group_crowd_query_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_group_crowd_query_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return bodyRes.getString("count");
    }
}
